package com.shanghaionstar.nav.presenter;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.map.common.PoiItemInfo;
import com.shanghaionstar.R;
import com.shanghaionstar.nav.iview.INavFavSaveView;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.nav.service.NavService;
import com.shanghaionstar.utils.MapHandler;

import rx.functions.Action1;

/**
 * Created by 6335yz on 2016/7/18.
 */
public class NavFavSavePresenter extends BasePresenter {

    private AbstractMapActivity mapActivity;
    private NavService navService;
    private INavFavSaveView navFavSaveView;

    public NavFavSavePresenter(IMVPScheduler mvpScheduler, AbstractMapActivity mapActivity, MapHandler mapHandler) {
        super(mvpScheduler);
        this.mapActivity = mapActivity;
        navService = new NavService(mapActivity);
    }

    @Override
    public void setView(IMVPView view) {
        super.setView(view);
        navFavSaveView = (INavFavSaveView) view;
    }

    public void addFavoriteDestination(String name, PoiItemInfo poiItemInfo) {
        if (validateInput(name)) {
            if (navFavSaveView != null) {
                navFavSaveView.setLoading(true);
            }
            subscription = navService.doAddFavoriteDestination(name, poiItemInfo)
                    .subscribeOn(mvpScheduler.backgroundThread())
                    .observeOn(mvpScheduler.mainThread())
                    .subscribe(new Action1<String>() {
                        @Override
                        public void call(String s) {
                            if (navFavSaveView != null) {
                                navFavSaveView.setLoading(false);
                                navFavSaveView.onReponseSuccess("success");
                                navFavSaveView.showToast(mapActivity.getString(R.string.poi_save_destination_success));
                                navFavSaveView.closePage();
                            }
                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                            //里面抛出的Error信息
                            if (navFavSaveView != null) {
                                navFavSaveView.setLoading(false);
                                navFavSaveView.showToast(mapActivity.getString(R.string.nav_save_favorite_err));
                            }
                        }
                    });
        }
    }

    private boolean validateInput(String name) {
        if (CommonUtil.isNull(name)) {
            if (navFavSaveView != null) {
                navFavSaveView.onResponseError(new OnStarException("", mapActivity.getString(R.string.poi_not_empty)));
                return false;
            }
        }
        return true;
    }
}
